<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%
	String rolename = request.getSession().getAttribute("role_name") == null? ""
			: request.getSession().getAttribute("role_name").toString();
	String userid = request.getSession().getAttribute("user_id")==null?""
			:request.getSession().getAttribute("user_id").toString();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap core CSS -->
<link rel="stylesheet" type="text/css" href="../../bootstrap-3.3.7/dist/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="../../bootstrap-3.3.7/dist/css/bootstrap-theme.min.css" />
<link rel="stylesheet" type="text/css" href="../../css/index.css" />
<link rel="stylesheet" type="text/css" href="../../css/personal.css" />
<link rel="stylesheet" type="text/css" href="../../css/background.css" />

<script type="text/javascript" src="../../bootstrap-3.3.7/dist/js/jquery.min.js"></script>
<script type="text/javascript" src="../../bootstrap-3.3.7/dist/js/bootstrap.js"></script>
<!-- bootstrap表格插件 -->
<script src="../../bootstrap-3.3.7/dist/js/bootstrap-table.js"></script>
<link href="../../bootstrap-3.3.7/dist/css/bootstrap-table.css" rel="stylesheet" />
<script src="../../bootstrap-3.3.7/dist/js/bootstrap-table-zh-CN.js"></script>

<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<link href="../../bootstrap-3.3.7/docs/assets/css/ie10-viewport-bug-workaround.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
</head>
<body>
	<h4 class="heading">我的收藏</h4>
	<div class="myline"></div>
	<div class="col-md-12">
		<form id="formSearch" class="form-horizontal">
			<div class="form-group" style="margin-top: 15px; margin-bottom: 0px">
				<label class="control-label col-sm-1" for="txt_search_departmentname">题名</label>
				<div class="col-md-5">
					<input type="text" class="form-control" id="txt_search_departmentname">
				</div>
				<div class="col-md-1" style="text-align: left;">
					<button type="button" style="margin-left: 10px" id="btn_query" class="btn btn-primary">查询</button>
				</div>
				<div class="col-md-2" style="margin-left: 20px; text-align: left;">
					<button type="button" id="btn_delete" class="btn btn-primary">删除所选</button>
				</div>
			</div>
		</form>
	</div>
	<div class="col-md-12">
		<table id="tb_departments">
		</table>
	</div>

	<div class="modal fade" id="sendEmail" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
					<h4 class="modal-title" id="myModalLabel">请输入您要发送的邮箱</h4>
				</div>
				<div class="modal-body">
					<input id="toemail" name="toemail" type="text" class="form-control"> 
					<input id="name" name="name" type="text" hidden="true"> 
					<input id="url" name="url" type="text" hidden="true">
				</div>
				<div class="modal-footer">
					<button type="button" id="dismiss" class="btn btn-default" data-dismiss="modal">取消</button>
					<button type="button" class="btn btn-primary" onclick="send()">发送</button>
				</div>
			</div>
			<!-- /.modal-content -->
		</div>
		<!-- /.modal -->
	</div>


</body>
<script type="text/javascript">
	var role='<%=rolename%>';
	var userid='<%=userid%>';
	$(function() {

		//1.初始化Table
		var oTable = new TableInit();
		oTable.Init();

		//2.初始化Button的点击事件
		var oButtonInit = new ButtonInit();
		oButtonInit.Init();

	});

	var TableInit = function() {
		var oTableInit = new Object();
		//初始化Table
		oTableInit.Init = function() {
			$('#tb_departments')
					.bootstrapTable(
							{
								url : 'getCollection', //请求后台的URL（*）
								method : 'get', //请求方式（*）
								toolbar : '#toolbar', //工具按钮用哪个容器
								striped : true, //是否显示行间隔色
								cache : false, //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
								pagination : true, //是否显示分页（*）
								sortable : true, //是否启用排序
								sortOrder : "asc", //排序方式
								queryParams : oTableInit.queryParams,//传递参数（*）
								sidePagination : "server", //分页方式：client客户端分页，server服务端分页（*）
								pageNumber : 1, //初始化加载第一页，默认第一页
								pageSize : 10, //每页的记录行数（*）
								pageList : [10, 25, 50, 100], //可供选择的每页的行数（*）
								search : false, //是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大
								strictSearch : true,
								showColumns : true, //是否显示所有的列
								showRefresh : true, //是否显示刷新按钮
								minimumCountColumns : 2, //最少允许的列数
								clickToSelect : true, //是否启用点击选中行
								height : 650, //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
								uniqueId : "ID", //每一行的唯一标识，一般为主键列
								showToggle : true, //是否显示详细视图和列表视图的切换按钮
								cardView : false, //是否显示详细视图
								detailView : false, //是否显示父子表
								columns : [
										{
											checkbox : true
										},
										{
											field : 'name',
											title : '题名',
											width : '33%',
											formatter : function(value,row,index) {
												var element = "<a onclick='check(\""+row.id+"\")' data-id='"+ row.id+ "'><span class='text-primary'>"+ row.name+ "</span></a>&nbsp;&nbsp";

												return element;
											}
										},
										{
											field : 'author',
											title : '作者',
											width : '13%'
										},
										{
											field : 'part',
											title : '发布部门',
											width : '12%'
										},
										{
											field : 'time',
											title : '发布时间',
											width : '12%'
										},
										{
											field : 'library',
											title : '库',
											width : '10%'
										},
										{
											field : 'download',
											title : '下载',
											width : '5%'
										},
										{
											field : 'browse',
											title : '浏览',
											width : '5%'
										},
										{
											field : 'operate',
											title : '操作',
											width : '10%',
											formatter : function(value, row,
													index) {
												var element;
												element = '<img id="deleteimage" class="img-thumbnail" onclick="deleteone(\''+row.id+'\')" data-toggle="popover" data-trigger="hover" title="删除"  style="height: 20px; width: 20px; border: none; float: left" src="../../image/icon/delete.png">'
													     +'<img id="collectimage" class="img-thumbnail" onclick="down(\''+row.url+'\',\''+row.id+'\',\''+row.level+'\')" data-toggle="popover" data-trigger="hover" title="下载" style="height: 20px; width: 20px; border: none; float: left" src="../../image/icon/download.png">'
/* 													     +'<img id="emailimage" data-id="'+ row.id+ '" data-toggle="modal" data-target="#sendEmail" data-url="'+row.url+'" data-name="'+row.name+'" class="img-thumbnail" data-toggle="popover" data-trigger="hover" title="发送到邮箱" style="height: 20px; width: 20px; border: none; float: left" src="../../image/icon/mail.png">';
 */												return element;
											}
										}]
							});
		};


		//得到查询的参数
		oTableInit.queryParams = function(params) {
			var temp = { //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
				limit : params.limit, //页面大小
				offset : params.offset, //页码初始开始值
				departmentname : $("#txt_search_departmentname").val(),
				statu : $("#txt_search_statu").val()
			};
			return temp;
		};
		return oTableInit;
	};

	var ButtonInit = function() {
		var oInit = new Object();
		oInit.Init = function() {
			//初始化页面上面的按钮事件
			$('#btn_query').bind('click', function() {
				$('#tb_departments').bootstrapTable('refreshOptions',{pageNumber:1});
			});
			$('#btn_delete').bind(
					'click',
					function() {
						var deleteRows = $('#tb_departments').bootstrapTable(
								"getSelections");
						if (deleteRows.length == 0) {
							alert("请选择要删除的数据");
							return;
						}
						var strIds = [];
						for (var i = 0; i < deleteRows.length; i++) {
							strIds.push(deleteRows[i].id);
						}
						var ids = strIds.join(",");
						if (confirm("您确定删除这" + deleteRows.length + "条数据吗")) {
							$.ajax({
								type : 'POST',
								url : 'deleteCollections',
								data : {
									ids : ids
								},
								success : function(result) {
									$('#tb_departments').bootstrapTable(
											"refresh", {
												url : "getCollection"
											});
								}
							});
						}
					});
		};

		return oInit;
	};
	function deleteone(row) {
		if (confirm("您确定删除这条数据吗")) {
			$.ajax({
				type : 'POST',
				url : 'deleteCollection',
				data : {
					id : row
				},
				success : function(result) {
					$('#tb_departments').bootstrapTable("refresh", {
						url : "getCollection"
					});
				}
			});
		}
	}
	function down(url,infoid,level) {
		var status="通过";
		if(role=="vip用户"||role=="知识管理员"||role=="超级知识管理员")
		{
			var url = encodeURI("../information/p_downloadn?filename=" + url+"&infoid="+infoid+"&status="+status);
			window.location.href =url;
		}
		else
		{
			if(level==2) {
				alert("该条知识是vip知识,您不具有下载权限");
				return;
			}
			else
			{
				var result = getJg();
				if(result==0){
					alert("您的积分不足，不能进行下载");
					return;
				}
				else
				{
				var url = encodeURI("../information/p_downloadn?filename=" + url+"&infoid="+infoid+"&status="+status);
				window.location.href =url;
				}
			}
		}
		
	}

	$('#sendEmail').on('show.bs.modal', function(event) {
		var button = $(event.relatedTarget) // 触发事件的按钮  
		var url = button.data('url') // 解析出url内容  
		var name = button.data('name') // 解析出name内容  
		var modal = $(this) //获得模态框本身
		modal.find('#name').val(name)
		modal.find('#url').val(url)
	});

	function send(){
		var name=$('#name').val();
		var url=$('#url').val();
		var toemail=$('#toemail').val();
		$.ajax({
			url:"../information/p_sendemail",
			async:false,
			data:encodeURI("filename=" + url+ "&truename=" + name+"&toemail="+toemail),
			error : function() {
				alert('请求失败');
			},
			success:function(result){
				if(result==1){
					alert("发送成功,如果收件箱中没有，请到垃圾箱中查找");
				}
				else{
					alert("发送失败,文件可能不存在");
				}
				
			}
		});
	}
	//查看知识详情
	function check(id) {
		window.open("../information/getInfoDetial?info_id="+id);
	}
	//查找积分是否允许下载
	 function getJg(){
		   var jg = 0;
			$.ajax({
				url:"../information/getPermitload",
				async:false,
				data:"userid="+userid,
				success:function(result){
					jg= result;
				}
			});
			return jg;
	 }
</script>
</html>